﻿
Namespace internalFunctions
    'parameters, msg, any, convert to string, error message
    '            type, optional, int / var, error type, default value is information
    '            typechar, optional, string, use the first char, if type is other, use this as error type
    'return, bool, success or not
    Public Class _raiseError
        Inherits internalFunction

        Public Class parameters
            Public Const msg As String = "msg"
            Public Const type As String = "type"
            Public Const typechar As String = "typechar"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.msg, parameters.type, parameters.typechar)
        End Sub

        Public Sub New()
            MyBase.New("raiseError", m)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim msg As String = Nothing
            If ctx.findVariableStr(parameters.msg, msg) Then
                Dim type As Int64
                If Not ctx.findVariableInt(parameters.type, type) Then
                    type = errorHandle.errorType.information
                End If
                Dim typecharStr As String = Nothing
                typecharStr = ctx.findVariableStr(parameters.typechar)
                Dim typechar As Char = character.null
                If Not isemptystring(typecharStr) Then
                    typechar = typecharStr(0)
                End If
                If Not validErrorType(type) Then
                    type = errorHandle.errorType.warning
                End If
                raiseError(msg + ", @" + context.callingcode(caller), CType(type, errorHandle.errorType), typechar)
                Return True
            Else
                raiseError("expect parameter " + parameters.msg, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class
End Namespace